package com.example.smartcity_poverty.ui.AllService.NumberBook

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.model.NumberBook.NBListModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.NumberBook.activity.NumberBookShowActivity
import kotlinx.android.synthetic.main.activity_number_book.*
import kotlinx.android.synthetic.main.re_number_book_list.*

/**
 * 数字图书馆 模块开发
 */
class NumberBookActivity : AppCompatActivity() {

    private val vm by lazy {
        ViewModelProviders.of(this)[NumberBookViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_number_book)
        title = "数字图书馆"


        // 填充数据
        setAdapter()
    }

    /**
     * 填充数据
     */
    private fun setAdapter() {
        vm.setToken(
            getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString())
        vm.nBookLiveData.observe(this, Observer { resources ->
            resources.getOrNull()?.apply {
                vm.nBookList.clear()
                vm.nBookList.addAll(this.rows!!)
                if (this.code == 200){
                    NumberBookRv.apply {
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = NumberBookAdapter(vm.nBookList, R.layout.re_number_book_list)
                    }
                }else "加载图书馆列表失败！".show()
            }
        })
    }


    /**
     * 图书馆列表适配器
     */
    inner class NumberBookAdapter(
        val list: MutableList<NBListModel.RowsBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<NBListModel.RowsBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: NBListModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind
            val con = v.view?.context

            glide(url + d.imgUrl, v.nbImage)
            v.nbTitle.text = d.name
            v.nbThis.text = "详细地址: ${d.address}"
            v.nbTime.text = "营业时间: ${d.businessHours}"
            if (d.businessState.toString() == "1") v.nbIsOk.text = "营业状态: 营业中..."
            else v.nbIsOk.text = "营业状态: 关门..."


            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, NumberBookShowActivity::class.java).putExtra("id", d.id.toString()))
            }
        }

    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}